import json
"""
json.dump：写入json内容
"""
data = {
    	'nanbei':'haha',
        'a':[1,2,3,4],
        'b':(1,2,3) }
with open('json_test.txt','w') as f:
    """
    json_test.txt ：表示文件的名字。如果前面没有接路径，那么就是放到这个文件的同级目录保存
    w：表示写入，往json_test.txt这个文件里面写入内容。w是覆盖写入，之前的内容都会覆盖
    """
    json.dump(data,f)
    """
    写入内容，data是具体写入的数据，f表示这个文件
    """
"""
json.load() 读取
"""
"""
with open("json_test.txt","r+") as f:
    print(json.load(f))
"""
# 绝对路径读取文件内容
"""
f = open(r'E:\21st-issue-courseware\day6\json_test.txt',encoding='utf-8')
print(f.read())
f.close() # 如果是使用变量赋值的方式打开文件，必须记得关闭
"""
# 相对路径读取文件内容
"""
f = open('json_test.txt',encoding='utf-8')
print(f.read())
f.close()
"""
# 上下文管理：读取文件
"""
with open('json_test.txt',"r",encoding='utf-8') as f :
    print(f.read())
"""
# 只写模式：w，覆盖写入，之前的都会不存在
"""
name = input("请输入学生姓名")
with open("student.txt",'w',encoding='utf-8') as f:
    f.write(name + "\n" + "博文") 
with open("student.txt",'w',encoding='utf-8') as f:
    f.writelines(['博文\n','宇浩\n','瀚文'])
"""
# 只读模式：r
"""
with open('student.txt','r',encoding='utf-8') as f:
    print(f.read())
    # print(f.readlines())
"""
# 追加写入：a
"""
with open('student.txt','a',encoding='utf-8') as f:
    f.write("\n田婉潼")
"""



